D:\a\tools.proto\tools.proto\compiler\src\gen\rust\union.rs
Line | Count | Source |
1 | | // Copyright (c) 2024, BlockProject 3D |
2 | | // |
3 | | // All rights reserved. |
4 | | // |
5 | | // Redistribution and use in source and binary forms, with or without modification, |
6 | | // are permitted provided that the following conditions are met: |
7 | | // |
8 | | // * Redistributions of source code must retain the above copyright notice, |
9 | | // this list of conditions and the following disclaimer. |
10 | | // * Redistributions in binary form must reproduce the above copyright notice, |
11 | | // this list of conditions and the following disclaimer in the documentation |
12 | | // and/or other materials provided with the distribution. |
13 | | // * Neither the name of BlockProject 3D nor the names of its contributors |
14 | | // may be used to endorse or promote products derived from this software |
15 | | // without specific prior written permission. |
16 | | // |
17 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 | | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 | | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 | | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
24 | | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
25 | | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 | | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | |
29 | | use crate::compiler::union::{DiscriminantField, Union}; |
30 | | use crate::compiler::util::types::{Name, TypePathMap}; |
31 | | use crate::gen::base::map::{DefaultTypeMapper, TypePathMapper}; |
32 | | use crate::gen::base::union::{generate, Utilities}; |
33 | | use crate::gen::rust::util::RustUtils; |
34 | | use crate::gen::template::hook::TemplateHooks; |
35 | | use crate::gen::template::{Options, Template}; |
36 | | use crate::gen::RustParams; |
37 | | use itertools::Itertools; |
38 | | |
39 | | const TEMPLATE: &[u8] = include_bytes!("./union.template"); |
40 | | |
41 | | impl Utilities for RustUtils { |
42 | 6 | fn gen_discriminant_path(discriminant: &DiscriminantField) -> String { |
43 | 6 | discriminant |
44 | 6 | .iter() |
45 | 8 | .map(|(f, is_leaf)| { |
46 | 8 | if is_leaf { Branch (46:20): [True: 6, False: 2]
Branch (46:20): [Folded - Ignored]
|
47 | 6 | format!("raw().get_{}()", f.name) |
48 | | } else { |
49 | 2 | format!("get_{}()", f.name) |
50 | | } |
51 | 8 | }) |
52 | 6 | .join(".") |
53 | 6 | } |
54 | | |
55 | 6 | fn gen_discriminant_path_mut(discriminant: &DiscriminantField) -> String { |
56 | 6 | discriminant |
57 | 6 | .iter() |
58 | 8 | .map(|(f, is_leaf)| { |
59 | 8 | if is_leaf { Branch (59:20): [True: 6, False: 2]
Branch (59:20): [Folded - Ignored]
|
60 | 6 | format!("raw_mut().set_{}", f.name) |
61 | | } else { |
62 | 2 | format!("get_{}_mut()", f.name) |
63 | | } |
64 | 8 | }) |
65 | 6 | .join(".") |
66 | 6 | } |
67 | | } |
68 | | |
69 | 6 | fn get_generics(u: &Union) -> (&str, &str) { |
70 | 10 | if u.cases.iter().any(|v| v.item_type.is_some())6 { Branch (70:8): [True: 4, False: 2]
Branch (70:8): [Folded - Ignored]
|
71 | 4 | ("<'a>", "<'_>") |
72 | | } else { |
73 | 2 | ("", "") |
74 | | } |
75 | 6 | } |
76 | | |
77 | 6 | pub fn gen_union_decl(u: &Union, type_path_map: &TypePathMap, params: &RustParams) -> String { |
78 | 6 | let mut hooks = TemplateHooks::new(); |
79 | 6 | if params.enable_write_async { Branch (79:8): [True: 1, False: 5]
Branch (79:8): [Folded - Ignored]
|
80 | 1 | hooks.hook("write_to", "write_to_async"); |
81 | 5 | } |
82 | 6 | let mut options = Options::default(); |
83 | 6 | if params.disable_read.contains(u.name()) { Branch (83:8): [True: 0, False: 6]
Branch (83:8): [Folded - Ignored]
|
84 | 0 | options.disable("from_bytes").disable("getters"); |
85 | 6 | } |
86 | 6 | if params.disable_write.contains(u.name()) { Branch (86:8): [True: 0, False: 6]
Branch (86:8): [Folded - Ignored]
|
87 | 0 | options.disable("write_to").disable("write_to_async").disable("setter"); |
88 | 6 | } |
89 | 6 | if !params.enable_union_set_discriminant { Branch (89:8): [True: 4, False: 2]
Branch (89:8): [Folded - Ignored]
|
90 | 4 | options.disable("setter"); |
91 | 4 | }2 |
92 | 6 | if params.disable_read.contains(u.name()) && params.disable_write.contains(u.name())0 { Branch (92:8): [True: 0, False: 6]
Branch (92:50): [True: 0, False: 0]
Branch (92:8): [Folded - Ignored]
Branch (92:50): [Folded - Ignored]
|
93 | 0 | options.disable("decl"); |
94 | 0 | hooks.hook("decl", "empty"); |
95 | 6 | } else { |
96 | 6 | hooks.hook("decl_unique", "from_value"); |
97 | 6 | } |
98 | 6 | let mut template = Template::compile_with_options(TEMPLATE, &options).unwrap(); |
99 | 6 | let (simple, elided) = get_generics(u); |
100 | 6 | template.var("generics", simple).var("elided_generics", elided); |
101 | 6 | generate::<RustUtils, _>( |
102 | 6 | template, |
103 | 6 | u, |
104 | 6 | &TypePathMapper::new(type_path_map, DefaultTypeMapper), |
105 | 6 | &hooks, |
106 | 6 | ) |
107 | 6 | } |